package com.example.lin.day10_9_service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.IOException;

public class MyService2 extends Service {

    private static final String TAG = MyService2.class.getSimpleName();
    private MediaPlayer mediaPlayer;

    /**
     * 当 使用 bindService 启动服务的时候,该方法被调用
     *
     * @param intent
     * @return 返回 IBinder的引用对象(为了实现数据交互)
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: intent " + intent);
        return new MyBinder();
    }


    private MainActivity2.MusicProgressCallback callback;


    public class MyBinder extends Binder {


        public void setMusicProgressCallback(MainActivity2.MusicProgressCallback callback) {
            MyService2.this.callback = callback;
        }

        public void a(String a) {
            playMusic();
        }

        public void b() {
            pauseMusic();
        }

        public void c() {
            nextSong();
        }

        public void seek(int s) {
            mediaPlayer.seekTo((int) (s / 100.0 * mediaPlayer.getDuration()));
        }

    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.i(TAG, "onRebind: intent " + intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "onUnbind: intent " + intent);
        return true;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    if (mediaPlayer != null) {
                        SystemClock.sleep(800);
                        int duration = mediaPlayer.getDuration();
                        int currentPosition = mediaPlayer.getCurrentPosition();
                        int p = (int) (100.0 * currentPosition / duration);
                        callback.onProgress(p);
                    }
                }

            }
        }).start();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }


    int i = 1;

    private Handler h = new Handler();


    private void playMusic() {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(this, Uri.parse("http://96.ierge.cn/11/168/336613.mp3"));


        }
        int duration = mediaPlayer.getDuration();

        int currentPosition = mediaPlayer.getCurrentPosition();

        Log.d(TAG, "playMusic: " + currentPosition);
        Log.d(TAG, "playMusic: " + duration);
        mediaPlayer.start();


//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                for (; i <= 100 && !isPause; i++) {
//                    SystemClock.sleep(400);
//                    Log.i(TAG,
//                            "playMusic: 春哥在后台唱歌..." + i);
//                    h.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            callback.onProgress(i);
//                        }
//                    });
//                }
//            }
//        }).start();
    }

    private boolean isPause = false;

    private void pauseMusic() {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
        }


    }

    private void nextSong() {
        Log.i(TAG, "pauseMusic: 春哥切歌了.......");
    }

}
